/**
 * Pizza delivery prompt example
 * run example by writing `node pizza.js` in your console
 */

'use strict';

// const NODE_ENV = process.env.NODE_ENV;

const inquirer = require('inquirer');
const config =  require('./src/app.config.js');


const path = require('path');
const fs = require('fs');

let {branches} = config;


var questions = [
  {
    type: 'list',
    name: 'branch',
    message: '请选择需要的主题色?',
    choices: branches,
    filter: function(val) {
        return val.toLowerCase();
    }
  }
];

inquirer.prompt(questions).then(answers => {
  console.log(JSON.stringify(answers, null, '  '));
  
  var data = fs.readFileSync(path.join(__dirname,'./src/main.template.ts'),'utf8');
  let mainJs = data.replace("/* variable-sass */", `require('./assets/sass/${answers.branch}-theme.scss')`);

  // if(NODE_ENV === 'development'){
    
  //   mainJs = data.replace("/* variable-sass */", `require('./styles.css')`);
  // }

  fs.writeFile('./src/main.ts',mainJs,( err ) => {
       if(err) throw err;
       fs.writeFile('./src/app.environment.js',getEnvironment(answers.branch), ( err ) => {
            if(err) throw err;
            console.log("\n配置模板完成,准备进行打包！")
        })
  })

})


function getEnvironment(name) {
  console.log( "环境" + process.env.NODE_ENV)
   return `module.exports = { CURRENT : '${name}', environment:  "${process.env.NODE_ENV}"}`
}

